package com.qut.graduate.busrtquery.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.search.core.RouteLine;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.qut.graduate.busrtquery.BRTQConstant;
import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;
import com.qut.graduate.busrtquery.utils.uiabout.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

import am.widget.wraplayout.WrapLayout;

/**
 * 换乘界面的recyclerView的适配器
 */
public class TransferPathPlanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "TransferPathPlanAdapter";
    private int mType;
    private Context mContext;
    private List<? extends RouteLine> mtransitRouteLines;
    private int duration; // 具体的时间，但需要换算
    private String convertTime; // 换算后的时间
    private int distance; // 具体的距离，但需要换算
    private double convertDistance; // 换算后的距离，以公里为单位

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

    /**
     * @param context
     * @param transitRouteLines
     * @param type              路线类型
     */
    public TransferPathPlanAdapter(Context context, List<? extends RouteLine> transitRouteLines, int type) {
        mType = type;
        mContext = context;
        mtransitRouteLines = transitRouteLines;
    }

    @Override
    public int getItemViewType(int position) {
        if (mType == BRTQConstant.TRANSIT_ROUTE) {
            return 0;
        } else {
            return 1;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_transfer_path_plan, parent, false);
            return new TransferPathPlanAdapter.BusPathPlanHolder(view);
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onClick(holder.itemView, position);
                }
            });
        }
        distance = mtransitRouteLines.get(position).getDistance();
        convertDistance = (Math.round(distance / 100d) / 10d);
        duration = mtransitRouteLines.get(position).getDuration();
        if (duration / 3600 == 0) convertTime = duration / 60 + "分钟";
        else convertTime = duration / 3600 + "小时" + (duration % 3600) / 60 + "分钟";

        if (holder instanceof TransferPathPlanAdapter.BusPathPlanHolder) {
            TransferPathPlanAdapter.BusPathPlanHolder busPathPlanHolder = (TransferPathPlanAdapter.BusPathPlanHolder) holder;
            // 强制关闭viewholder复用
            busPathPlanHolder.setIsRecyclable(false);
            TransitRouteLine transitRouteLine = (TransitRouteLine) mtransitRouteLines.get(position);

            busPathPlanHolder.duration.setText(convertTime);
            busPathPlanHolder.distance.setText(convertDistance + "公里");

            TransitRouteLine.TransitStep step1 = null;
            TransitRouteLine.TransitStep step2 = null;
            String s2;
            String s1;
            String station;
            String first_station = null;
            int station_sum = 0;
            List<String> listbus = new ArrayList<String>();
            List<String> listwalk = new ArrayList<String>();

            step1 = transitRouteLine.getAllStep().get(0);
            s1 = step1.getInstructions();
            if (s1.contains("到达") && s1.contains("站")) {
                first_station = s1.substring(s1.indexOf("达") + 1, s1.indexOf("站") + 1);
            }
            for (int i = 0; i < transitRouteLine.getAllStep().size(); i++) {
                step1 = transitRouteLine.getAllStep().get(i);
                s1 = step1.getInstructions();
                Log.w("规划详情:", s1);
                if (step1.getStepType() != TransitRouteLine.TransitStep.TransitRouteStepType.WAKLING) {
                    s2 = s1.substring(s1.indexOf("坐") + 1, s1.indexOf(","));
                    listbus.add(s2);
                } else {
                    if (s1.contains(",")) {
                        s2 = s1.substring(0, s1.indexOf(","));
                        listwalk.add(s2);
                    }
                }
            }
            for (int i = 0; i < transitRouteLine.getAllStep().size(); i++) {
                step2 = transitRouteLine.getAllStep().get(i);
                station = step2.getInstructions();
                if (step2.getStepType() != TransitRouteLine.TransitStep.TransitRouteStepType.WAKLING) {
                    if (station.contains("地铁")) {
                        s2 = station.substring(station.indexOf("过") + 1);
                        if (s2.contains("站")) {
                            s2 = s2.substring(0, s2.indexOf("站"));
                            station_sum += Integer.parseInt(s2);
                        } else {
                            station_sum += Integer.parseInt(s2);
                        }
                        Log.w("站数:", String.valueOf(station_sum));
                    } else if (station.contains("路")) {
                        s2 = station.substring(station.indexOf("过") + 1);
                        if (s2.contains("站")) {
                            s2 = s2.substring(0, s2.indexOf("站"));
                            station_sum += Integer.parseInt(s2);
                        } else {
                            station_sum += Integer.parseInt(s2);
                        }
                        Log.w("站数:", String.valueOf(station_sum));
                    }
                }
            }
            busPathPlanHolder.stationNum.setText(station_sum + "站");
            busPathPlanHolder.startStation.setText(first_station + "上车");
            if (listbus.size() - 1 > 0)
                busPathPlanHolder.transferNum.setText("换乘" + String.valueOf(listbus.size() - 1) + "次");
            else busPathPlanHolder.transferNum.setVisibility(View.GONE);

            for (int i = 0; i < listbus.size(); i++) {
                if (i > 0) {
                    ImageView imageView = new ImageView(mContext);
                    imageView.setImageResource(R.mipmap.icon_transfer);
                    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                    params.height = ScreenUtils.dp2px(20);
                    params.width = ScreenUtils.dp2px(20);
                    params.setMargins(ScreenUtils.dp2px(10), 5, 0, 5);
                    imageView.setLayoutParams(params);
                    busPathPlanHolder.busWrapLayout.addView(imageView);
                }
                ImageView imageView = new ImageView(mContext);
                if (listbus.get(i).contains("地铁")) {
                    imageView.setImageResource(R.mipmap.icon_subway);
                } else {
                    imageView.setImageResource(R.mipmap.icon_bus);
                }
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                params.height = ScreenUtils.dp2px(20);
                params.width = ScreenUtils.dp2px(20);
                params.setMargins(0, 5, 0, 5);
                imageView.setLayoutParams(params);
                busPathPlanHolder.busWrapLayout.addView(imageView);

                TextView showText = new TextView(mContext);
                showText.setTextColor(Color.BLACK);
                showText.setTextSize(12);
                showText.setText(listbus.get(i));
                LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
                params1.setMargins(0, 5, 0, 5);
                showText.setLayoutParams(params1);
                busPathPlanHolder.busWrapLayout.addView(showText);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mtransitRouteLines!=null ? mtransitRouteLines.size() : 0;
    }

    public class BusPathPlanHolder extends RecyclerView.ViewHolder {
        public WrapLayout busWrapLayout; // 添加信息的控件
        public TextView duration; // 到达需要时间
        public TextView distance; // 与目的地的距离
        public TextView stationNum; // 总共的公交站数
        public TextView transferNum; // 换乘次数
        public TextView startStation; // 上车所在站点

        BusPathPlanHolder(View view) {
            super(view);
            busWrapLayout = view.findViewById(R.id.bus_wrapLayout);
            duration = view.findViewById(R.id.duration);
            distance = view.findViewById(R.id.distance);
            stationNum = view.findViewById(R.id.stationNum);
            transferNum = view.findViewById(R.id.transferNum);
            startStation = view.findViewById(R.id.startStation);
        }
    }

}
